package com.sjq.supplier.controller;


import com.sjq.supplier.entity.Order;
import com.sjq.supplier.service.OrderService;
import com.sjq.supplier.util.OrderGenerator;
import com.sjq.supplier.util.Result;
import lombok.extern.slf4j.Slf4j;
import com.sjq.supplier.entity.Accomplish;
import com.sjq.supplier.service.AccomplishService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Accomplish)表控制层
 *
 * @author makejava
 * @since 2024-01-02 14:54:49
 */
@Slf4j
@RestController
@RequestMapping("accomplish")
public class AccomplishController {
    /**
     * 服务对象
     */
    @Resource
    private AccomplishService accomplishService;
    @Resource
    private OrderService orderService;

    /**
     * 分页查询所有数据
     *
     * @param accomplish 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public Result selectAll(Accomplish accomplish, Integer limit, Integer page,Integer state) {
        if (limit == null || page == null) {
            return Result.failure("limit和page不能为空");
        }
        return accomplishService.selectAll(accomplish,limit,page,state);
    }

   

    /**
     * 新增数据
     *
     * @param accomplish 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    @Transactional(rollbackFor = Exception.class)
    public Result insert(Accomplish accomplish,Integer orderid) {
        //判断orderid是否为空
        if(orderid==null || orderid==0){
            return Result.failure("订单id不能为空");
        }

        Order byId = orderService.getById(orderid);
        accomplish.setTitle(byId.getTitle());
        accomplish.setImg(byId.getImg());
        accomplish.setPrice(byId.getPrice());
        accomplish.setGoodsid(byId.getGoodsid());
        accomplish.setSupplierid(byId.getSupplierid());
        accomplish.setIsvalid(1);
        accomplish.setStock(byId.getStock());
        accomplish.setSumprice(byId.getSumprice());
        accomplish.setOrderid(byId.getOrderid());
        accomplish.setUserid(byId.getUserid());
        Long logisticsnum = accomplish.getLogisticsnum();
        //判断logisticsnum是否为空
        if(logisticsnum==null || logisticsnum==0){
            accomplish.setLogisticsnum(OrderGenerator.generateOrderNumber());
        }

        if (accomplishService.save(accomplish)){
            //修改订单状态
            byId.setState(1);
            orderService.updateById(byId);
            return Result.success("发货成功");
        }
        return Result.failure("发货失败");
    }

}

